package com.jianjun.ktui.ui.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.savedstate.SavedState
import androidx.savedstate.savedState
import androidx.savedstate.write
import org.jetbrains.compose.ui.tooling.preview.Preview


/**
 *
 * @author source
 * {@code @date} 2025/3/27 09:30
 */
@OptIn(ExperimentalLayoutApi::class)
@Composable
fun OnlineScreen(
    showSnackbar: (String) -> Unit,
    uiState: SavedState,
    onQueryClicked: () -> Unit,
    rows: MutableList<MutableMap<String, Any>>
) {
    val heads = listOf(
        "序号", "会话编号", "登录名称", "所属部门", "主机", "登录地点", "操作系统", "浏览器", "登录时间", "操作"
    )
    var ipaddr by remember { mutableStateOf("") }
    var userName by remember { mutableStateOf("") }
    Column {
        FlowRow(
            modifier = Modifier.padding(10.dp),
            horizontalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            OutlinedTextField(
                value = ipaddr,
                label = {
                    Text("登录地址")
                },
                placeholder = {
                    Text("请输入登录地址")
                },
                onValueChange = {
                    ipaddr = it
                },
                singleLine = true
            )
            OutlinedTextField(
                value = userName,
                label = {
                    Text("用户名称")
                },
                placeholder = {
                    Text("请输入用户名称")
                },
                onValueChange = {
                    userName = it
                },
                singleLine = true
            )
            Button(
                modifier = Modifier.align(Alignment.CenterVertically),
                onClick = {
                    uiState.write {
                        putString("ipaddr", ipaddr)
                        putString("userName", userName)
                    }
                    onQueryClicked()
                }
            ) {
                Icon(Icons.Filled.Search, null)
                Text("查询")
            }
            Button(
                modifier = Modifier.align(Alignment.CenterVertically),
                onClick = {
                    ipaddr = ""
                    userName = ""
                }
            ) {
                Icon(Icons.Filled.Refresh, null)
                Text("重置")
            }
        }
        SelectionContainer {
            Column(modifier = Modifier.padding(10.dp)) {
                Row(modifier = Modifier.background(MaterialTheme.colorScheme.secondaryContainer)) {
                    heads.forEach {
                        Text(it, textAlign = TextAlign.Center, modifier = Modifier.weight(1f))
                        VerticalDivider()
                    }
                }
                HorizontalDivider()
                rows.forEach {
                    Row {
                        Text("", textAlign = TextAlign.Center, modifier = Modifier.weight(1f))
                        VerticalDivider()
                        Text(
                            it["tokenId"] as? String ?: "",
                            textAlign = TextAlign.Center,
                            modifier = Modifier.weight(1f)
                        )
                        VerticalDivider()
                        Text(
                            it["userName"] as? String ?: "",
                            textAlign = TextAlign.Center,
                            modifier = Modifier.weight(1f)
                        )
                        VerticalDivider()
                        Text(
                            it["deptName"] as? String ?: "",
                            textAlign = TextAlign.Center,
                            modifier = Modifier.weight(1f)
                        )
                        VerticalDivider()
                        Text(
                            it["ipaddr"] as? String ?: "",
                            textAlign = TextAlign.Center,
                            modifier = Modifier.weight(1f)
                        )
                        VerticalDivider()
                        Text(
                            it["loginLocation"] as? String ?: "",
                            textAlign = TextAlign.Center,
                            modifier = Modifier.weight(1f)
                        )
                        VerticalDivider()
                        Text(it["os"] as? String ?: "", textAlign = TextAlign.Center, modifier = Modifier.weight(1f))
                        VerticalDivider()
                        Text(
                            it["browser"] as? String ?: "",
                            textAlign = TextAlign.Center,
                            modifier = Modifier.weight(1f)
                        )
                        VerticalDivider()
                        Text(
                            it["loginTime"] as? String ?: "",
                            textAlign = TextAlign.Center,
                            modifier = Modifier.weight(1f)
                        )
                        VerticalDivider()
                        Text("操作", textAlign = TextAlign.Center, modifier = Modifier.weight(1f))
                        VerticalDivider()
                    }
                    HorizontalDivider()
                }
            }
        }
    }
    //开始进入默认点击
    if (rows.isEmpty()) {
        uiState.write {
            putString("ipaddr", ipaddr)
            putString("userName", userName)
        }
        onQueryClicked()
    }
}

@Preview(widthDp = 700, heightDp = 500)
@Composable
fun OnlineScreenPreview() {
    OnlineScreen(
        showSnackbar = {},
        uiState = savedState(),
        onQueryClicked = {},
        rows = mutableListOf()
    )
}